<?
class db_init
{
	var $server;
	var $username;
	var $password;
	var $database;
	
	function db_init()
	{
		// Khai bao Server o day
		$this->server = 'localhost';
		$this->username = 'root';
		$this->password = '';
		$this->database = 'mynghe.com';
	}
	
	function __destruct()
	{
		unset($this->server);
		unset($this->username);
		unset($this->password);
		unset($this->database);
	}
}

class db_query
{
	var $result;
	var $link;
	
	function db_query($query)
	{
		$db_init = new db_init();
		// Khai bao connect
		$this->link = @mysql_connect($db_init->server, $db_init->username, $db_init->password);
		
		if (!$this->link)
		{
			echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
			echo '<meta name="revisit-after" content="1 days">';
			echo "<center>";
			echo "Chào bạn, trang web bạn yêu cầu hiện chưa thể thực hiện được. <br>";
			echo "Xin bạn vui lòng đợi vài giây rồi ấn <b>F5 để Refresh</b> lại trang web <br>";
			echo "</center>";
			exit();
		}
		
		$db_select = mysql_select_db($db_init->database, $this->link);
		
//		echo $query;
//		$time_start = $this->microtime_float();
		
		mysql_query("SET NAMES 'utf8'");
		$this->result = mysql_query($query,$this->link);
		
//		$time_end = $this->microtime_float();
//		$time = $time_end - $time_start;
//		echo " <font color='red'><b>" . number_format($time,10,".",",") . "</b></font> ";	
		
		unset($db_init);
		if (!$this->result)
		{
			$error = mysql_error($this->link);
			mysql_close($this->link);	
			die('Error in query string ' . $error);
		}
	}
	
	function close()
	{
		mysql_free_result($this->result); 
		if ($this->link) 
		{  
			mysql_close($this->link);		
		}
	}
	
	//Hàm tinh time
	function microtime_float()
	{
	   list($usec, $sec) = explode(' ', microtime());
	   return ((float)$usec + (float)$sec);
	}
}

class db_execute 
{
	var $link;
	
	function db_execute($query)
	{
		$db_init = new db_init();
		
		$this->link = mysql_connect($db_init->server, $db_init->username, $db_init->password);
		mysql_select_db($db_init->database);
		
		unset($db_init);
		
		mysql_query("SET NAMES 'utf8'");
		mysql_query($query);
		mysql_close($this->link);
	}
}

class db_execute_return 
{
	var $link;
	var $result;
	
	function db_execute($query)
	{
		$db_init = new db_init();
		
		$this->link = mysql_connect($db_init->server, $db_init->username, $db_init->password);
		mysql_select_db($db_init->database);
		
		unset($db_init);
		
		mysql_query("SET NAMES 'utf8'");
		mysql_query($query);
		
		$last_id = 0;
		$this->result = mysql_query('SELECT LAST_INSERT_ID() AS last_id', $this->link);
		if ($row = mysql_fetch_array($this->result))
		{
			$last_id = $row['last_id'];
		}
		
		mysql_close($this->link);
		
		return $last_id;
	}
}